package com.itqf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itqf.bean.DtsStorage;
import com.itqf.bean.DtsStorageExample;
import com.itqf.dto.CommonsDto;
import com.itqf.mapper.DtsStorageMapper;
import com.itqf.service.DtsStorageService;
import com.itqf.utils.AliyunOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * projectName: gp_manage
 *
 * @author: 赵伟风
 * time: 2021/8/23 14:35
 * description:
 */
@Service
public class DtsStorageServiceImpl implements DtsStorageService {

    @Autowired
    private DtsStorageMapper dtsStorageMapper;

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;



    /**
     * 存储数据列表+模糊查询
     *
     * @param commonsDto
     * @return
     */
    @Override
    public Object findStorageByPage(CommonsDto commonsDto) {

        //1.分页设置
        PageHelper.startPage(commonsDto.getPage(), commonsDto.getLimit());

        //2.查询数据[条件]

        DtsStorageExample example = new DtsStorageExample();
        DtsStorageExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);

        if (commonsDto.getName() != null) {
            criteria.andNameLike("%"+commonsDto.getName()+"%");
        }
        if (commonsDto.getKey() != null)
        {
            criteria.andKeyLike("%"+commonsDto.getKey()+"%");
        }

        List<DtsStorage> dtsStorages = dtsStorageMapper.selectByExample(example);

        //3.pageInfo封装

        PageInfo<DtsStorage> pageInfo = new PageInfo<>(dtsStorages);

        //4.结果封装
        Map<String,Object> data = new HashMap<>();
        data.put("items", pageInfo.getList());
        data.put("total", pageInfo.getTotal());

        return data;
    }

    /**
     * 文件存储
     * @param file
     * @return
     */
    @Override
    public DtsStorage insertStorage(MultipartFile file) throws Exception {


        String fileName = file.getOriginalFilename();

        String key = UUID.randomUUID().toString().replaceAll("-", "")+fileName;

        String suffName = fileName.substring(fileName.lastIndexOf("."));

        //1.上传阿里oss
        String url = AliyunOSSUtil.uploadImage(endpoint,accessKeyId,accessKeySecret,
               bucketName ,key,file.getBytes());

        //2.拼装dtsStorage
        DtsStorage dtsStorage = new DtsStorage();
        dtsStorage.setDeleted(false);
        dtsStorage.setAddTime(new Date());
        dtsStorage.setUrl(url);
        dtsStorage.setType(suffName);
        dtsStorage.setKey(key);
        dtsStorage.setName(fileName);
        dtsStorage.setSize((int) file.getSize());

        //3.传入数据库并回显
        dtsStorageMapper.insertDtsStorage(dtsStorage);

        return dtsStorage;
    }
}
